# -*- coding:utf-8 -*-
import xml.dom.minidom as minidom
from PIL import Image
import csv
import json
def csvtoxml(fname):
    with open(fname,encoding='utf-8')as f:
        reader = csv.reader(f)
        for row in reader:
            dom = minidom.getDOMImplementation().createDocument(None, 'annotation', None)
            root = dom.documentElement
            filename=row[4][9:]
            name=filename[:-4]
            image_path = 'F:\\tianchi\\1_images\\1_images\\' + filename  # 图片路径名
            img_pillow = Image.open(image_path)
            img_width = str(img_pillow.width)  # 图片宽度
            img_height = str(img_pillow.height)  # 图片高度
            # 文件夹名字
            element = dom.createElement('folder')
            element.appendChild(dom.createTextNode('1_images'))
            root.appendChild(element)

            # 文件名
            element = dom.createElement('filename')
            element.appendChild(dom.createTextNode(filename))
            root.appendChild(element)
            # 路径
            element = dom.createElement('path')
            element.appendChild(dom.createTextNode(image_path))
            root.appendChild(element)
            # source
            element_source = dom.createElement('source')
            element_source.appendChild(dom.createTextNode(''))
            root.appendChild(element_source)
            # source.database
            element = dom.createElement('database')
            element.appendChild(dom.createTextNode('Unknown'))
            element_source.appendChild(element)

            # size
            element_size = dom.createElement('size')
            element_size.appendChild(dom.createTextNode(''))
            root.appendChild(element_size)
            # size.width
            element = dom.createElement('width')
            element.appendChild(dom.createTextNode(img_height))
            element_size.appendChild(element)
            # size.height
            element = dom.createElement('height')
            element.appendChild(dom.createTextNode(img_width))
            element_size.appendChild(element)
            # size.depth
            element = dom.createElement('depth')
            element.appendChild(dom.createTextNode('3'))
            element_size.appendChild(element)

            # segmented
            element = dom.createElement('segmented')
            element.appendChild(dom.createTextNode('0'))
            root.appendChild(element)

            j = json.loads(row[5])["items"]
            for i in j:
                labels = i["labels"]["标签"]
                bbox = i["meta"]["geometry"]
                xmin,ymin,xmax,ymax = str(bbox[0]),str(bbox[1]),str(bbox[2]),str(bbox[3])
                # object
                element_root = dom.createElement('object')
                element_root.appendChild(dom.createTextNode(''))
                root.appendChild(element_root)
                # object.name
                element = dom.createElement('name')
                element.appendChild(dom.createTextNode(labels))  # 标签名
                element_root.appendChild(element)
                # object.pose
                element = dom.createElement('pose')
                element.appendChild(dom.createTextNode('Unspecified'))
                element_root.appendChild(element)
                # object.truncated
                element = dom.createElement('truncated')
                element.appendChild(dom.createTextNode('0'))
                element_root.appendChild(element)
                # object.difficult
                element = dom.createElement('difficult')
                element.appendChild(dom.createTextNode('0'))
                element_root.appendChild(element)
                # object.bndbox
                element_root_bbox = dom.createElement('bndbox')
                element_root_bbox.appendChild(dom.createTextNode(''))
                element_root.appendChild(element_root_bbox)
                # object.bndbox.xmin
                element = dom.createElement('xmin')
                element.appendChild(dom.createTextNode(xmin))
                element_root_bbox.appendChild(element)
                # object.bndbox.ymin
                element = dom.createElement('ymin')
                element.appendChild(dom.createTextNode(ymin))
                element_root_bbox.appendChild(element)
                # object.bndbox.xmax
                element = dom.createElement('xmax')
                element.appendChild(dom.createTextNode(xmax))
                element_root_bbox.appendChild(element)
                # object.bndbox.ymax
                element = dom.createElement('ymax')
                element.appendChild(dom.createTextNode(ymax))
                element_root_bbox.appendChild(element)

#输出地址
            with open("F:\\tianchi\\1_images\\labels\\"+name+'.xml', 'w', encoding='utf-8') as f:
                dom.writexml(f, addindent='\t', newl='\n', encoding='utf-8')
        return
    return
csvtoxml(r'1train_rname.csv')

# 保存文件
